package com.system.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.system.dao.TeacherDao;
import com.system.model.Teacher;
import com.system.util.JdbcUtil;
import com.system.util.ResultMapUtil;

public class TeacherDaoImpl extends BaseDaoImpl implements TeacherDao {

	public TeacherDaoImpl() throws Exception {
		super();
	}
	
	/**
	 * 对查找的重复操作进行封装
	 * @return
	 */
	private ArrayList<Teacher> find(String sql){
		try{
			ResultSet rs=this.templateQuery(sql);
			boolean find = false;
			ArrayList<Teacher> teachers = new ArrayList<>();
			while(rs.next()) {
				find = true;
				Teacher tea=new Teacher();
				Map<String,String> map=ResultMapUtil.getResultMap(rs);
				try{
					BeanUtils.populate(tea, map);
					teachers.add(tea);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			if(!find) {
				return null;
			}
			return teachers;
				
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public ArrayList<Teacher> findAll() {
		String sql="select * from "+"teacher";
		ArrayList<Teacher> teas=find(sql);
		JdbcUtil.getInstance().free(conn);
		return teas;
	}
	
	@Override
	public List<Teacher> findAll(int pageNum,int amount){
		List<Teacher> list=this.findAll();
		if(list.size()<amount*pageNum){
			
			return list.subList((pageNum-1)*amount, list.size());
		}
		else
			return list.subList((pageNum-1)*amount,amount*pageNum);
	}
	
	@Override
	public Teacher findById(String teanum) {
		String sql="select * from "+"teacher"+" where teanum="+"'"+teanum+"'";
		ArrayList<Teacher> teas=find(sql);
		Teacher tea = teas.get(0);
		JdbcUtil.getInstance().free(conn);
		return tea;
	}

	@Override
	public boolean add(Teacher tea) {
		String sql="insert into "+"teacher(user_id,teachername"+
				",major,teanum)"+ "values"+"("+
				"'"+tea.getUser_id()+"'"+
				","+"'"+tea.getTeachername()+"'"+
				","+"'"+tea.getMajor()+"'"+","
				+"'"+tea.getTeanum()+"'"+")";
		System.out.println(sql);
		try{
			this.templateUpdate(sql);
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			JdbcUtil.getInstance().free(conn);
		}
		return false;
	}

	@Override
	public boolean deleteTeachers(int[] teaIds) {
		try{
			for(int id:teaIds){
				String sql="delete from "+"teacher"+" where user_id="
						+id;
				this.templateUpdate(sql);
			}
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			JdbcUtil.getInstance().free(conn);
		}
		return false;
	}

	@Override
	public boolean modify(Teacher tea) {
		String sql="update teacher set teachername="+"'"+tea.getTeachername()+"'"+","
				+ " major="+"'"+tea.getMajor()+"'"+","
				+" teanum="+"'"+tea.getTeanum()+"'"
				+" where user_id="+tea.getUser_id();
		try{
			this.templateUpdate(sql);
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			JdbcUtil.getInstance().free(conn);
		}
		return false;
	}

	@Override
	public Integer findId(String teanum) {
		String sql="select id from teacher where teanum= "+"'"+teanum+"'";
		try {
			ResultSet rs=this.templateQuery(sql);
			int res = -1;
			if(rs.next()){
				res=(Integer)rs.getObject("id");
			}
			return res;
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return null;
	}
}
